package ${package.Controller};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.http.BusinessException;
import com.example.demo.common.http.RestResult;
import ${package.Entity}.${entity};
import ${package.Entity}.command.${entity}SaveCommand;
import ${package.Entity}.command.${entity}UpdateCommand;
import ${package.Entity}.converter.${entity}Converter;
import ${package.Entity}.resp.${entity}DetailResp;
import ${package.Entity}.resp.${entity}PageResp;
import ${package.Service}.${table.serviceName};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

import javax.annotation.Resource;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
    class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
@Api(tags = "${table.comment!}")
    <#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
public class ${table.controllerName} {
    </#if>

    @Resource
    private ${table.serviceName} ${table.serviceName?uncap_first};

    @PostMapping("/save")
    @ApiOperation(value = "新增${table.comment!}")
    public RestResult<String> save(@RequestBody @Validated ${entity}SaveCommand command) {
        ${entity} ${entity?uncap_first} = ${entity}Converter.INSTANCE.convert(command);
        ${table.serviceName?uncap_first}.save(${entity?uncap_first});
        return RestResult.getSuccessResult(String.valueOf(${entity?uncap_first}.getId()));
    }

    @PostMapping("/removeById")
    @ApiOperation(value = "根据id删除${table.comment!}")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "String")
    )
    public RestResult<Boolean> removeById(String id) {
        boolean success = ${table.serviceName?uncap_first}.removeById(Long.parseLong(id));
        return RestResult.getSuccessResult(success);
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新${table.comment!}")
    public RestResult<Boolean> updateById(@RequestBody @Validated ${entity}UpdateCommand command) {
        ${entity} ${entity?uncap_first} = ${entity}Converter.INSTANCE.convert(command);
        boolean success = ${table.serviceName?uncap_first}.updateById(${entity?uncap_first});
        return RestResult.getSuccessResult(success);
    }

    @PostMapping("/getById")
    @ApiOperation(value = "根据id查询${table.comment!}")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "String")
    )
    public RestResult<${entity}DetailResp> getById(String id) {
        ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.getById(Long.parseLong(id));
        if (null == ${entity?uncap_first}) {
            throw new BusinessException("id不存在");
        }
        ${entity}DetailResp resp = ${entity}Converter.INSTANCE.convert2DetailResp(${entity?uncap_first});
        return RestResult.getSuccessResult(resp);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询${table.comment!}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "Integer", paramType = "query")
    })
    public RestResult<Page<${entity}PageResp>> page(Integer pageNum, Integer pageSize) {
        Page<${entity}> page = ${table.serviceName?uncap_first}.page(new Page<>(pageNum, pageSize));
        Page<${entity}PageResp> resp = ${entity}Converter.INSTANCE.convert2PageResp(page);
        return RestResult.getSuccessResult(resp);
    }
}
</#if>
